package com.nativetalk.common.util;

import com.nativetalk.bean.study.TdNtStudyTeachUpdate;
import com.nativetalk.bean.teacher.TdNtTeacherGetui;
import com.nativetalk.common.Constants;
import com.nativetalk.common.getui.AndroidStudentGetui;
import com.nativetalk.common.getui.AndroidTeacherGetui;
import com.nativetalk.common.getui.IosStudentGetui;
import com.nativetalk.common.getui.IosTeacherGetui;
import com.nativetalk.dao.TdNtStudyTeachDao;
import com.nativetalk.dao.TdNtTeacherDao;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 16/1/7.
 */
public class FindTeacher extends Thread {
    private static Logger logger = Logger.getLogger(FindTeacher.class);

    public void run(TdNtStudyTeachDao tdNtStudyTeachDao, int sst_id, int cou_id, TdNtTeacherDao tdNtTeacherDao,
                    String memb_name, String memb_head_portrait, String cou_english_name, String type, String devicetoken, int version) {
        Map<String, TdNtTeacherGetui> tdNtTeachers = new HashMap<>();
        List<TdNtTeacherGetui> tdNtTeacherGetuisCached = new ArrayList<>();

        /**
         * 查找前20个老师
         */
        Map<String, Object> map = new HashMap<>();
        map.put("cou_id", cou_id);
        map.put("currentPage", 0);
        map.put("pageSize", 100);
        List<TdNtTeacherGetui> tdNtTeacherGetuis = tdNtTeacherDao.findTdNtTeacherByCouId(map);
        /**
         * 判断预计接通时间推送给前端用户
         */
        if (version >= 2) {
            int time;
            if (tdNtTeacherGetuis.size() >= 20) {
                time = 20;
            } else if (tdNtTeacherGetuis.size() >= 1) {
                time = 30;
            } else {
                time = 60;
            }
            if (devicetoken != null && !(devicetoken.equals(Constants.default_equ_id))) {
                if (type.equals("1")) {
                    AndroidStudentGetui.Countdown("外教君", "", devicetoken, time);
                } else {
                    IosStudentGetui.Countdown("外教君", "", devicetoken, time);
                }
            }
        }
        int i = 0;
        int a = 0;
        while (true) {
            try {
                TdNtStudyTeachUpdate tdNtStudyTeachDetail = tdNtStudyTeachDao.findTdNtStudyTeachUpdate(sst_id);
                i = i + 1;
                sleep(1 * 100);
                /**
                 * 判断是否等待两分钟或者是否已接单或已取消
                 */
                if (!(tdNtStudyTeachDetail.getStudy_state().equals("0")) || i >= 1190) {
                    break;
                } else {
                    /**
                     * 1.前五秒每次随机查询六个老师
                     * 2.五秒后每两秒查询一个老师
                     */
                    if (((i%10)==0) && ((i/10) < 5)) {

                        Map<String, Object> mapRound = new HashMap<>();
                        mapRound.put("cou_id", cou_id);
                        mapRound.put("pageSize", 6);
                        List<TdNtTeacherGetui> tdNtTeacherGetuisRound = tdNtTeacherDao.findTdNtTeacherByCouIdRound(mapRound);
                        for (int j = 0; j < tdNtTeacherGetuisRound.size(); j++) {
                            if (tdNtTeachers.get(tdNtTeacherGetuisRound.get(j).getTea_id()+"") == null) {
                                if (tdNtTeacherGetuisRound.get(j).getEqu_client_id() != null && !(tdNtTeacherGetuisRound.get(j).getEqu_client_id().equals(Constants.default_equ_id))) {

                                    if (tdNtTeacherGetuisRound.get(j).getEqu_type().equals("1")) {
                                        AndroidTeacherGetui.apnpush("NativeTalk", "One student sent you a teaching request, check now! ",
                                                tdNtTeacherGetuisRound.get(j).getEqu_client_id(), sst_id, memb_name, memb_head_portrait
                                                , tdNtTeacherGetuisRound.get(j).getToken(), tdNtTeacherGetuisRound.get(j).getTea_head_portrait(), tdNtTeacherGetuisRound.get(j).getTea_name(), cou_english_name);
                                        tdNtTeachers.put(tdNtTeacherGetuisRound.get(j).getTea_id() + "", tdNtTeacherGetuisRound.get(j));
                                        MemcachedUtils.add(tdNtTeacherGetuisRound.get(j).getTea_id() + "", tdNtTeachers, 3 * 60);
                                    } else {
                                        logger.info(tdNtTeacherGetuisRound.get(j).getEqu_client_id());
                                        IosTeacherGetui.apnpush("NativeTalk", "One student sent you a teaching request, check now! ",
                                                tdNtTeacherGetuisRound.get(j).getEqu_client_id(), sst_id, memb_name, memb_head_portrait
                                                , tdNtTeacherGetuisRound.get(j).getToken(), tdNtTeacherGetuisRound.get(j).getTea_head_portrait(), tdNtTeacherGetuisRound.get(j).getTea_name(), cou_english_name);
                                        tdNtTeachers.put(tdNtTeacherGetuisRound.get(j).getTea_id() + "", tdNtTeacherGetuisRound.get(j));
                                        MemcachedUtils.add(tdNtTeacherGetuisRound.get(j).getTea_id() + "", tdNtTeachers, 3 * 60);
                                    }
                                    tdNtTeacherGetuisCached.add(tdNtTeacherGetuisRound.get(j));
                                    if (MemcachedUtils.get(tdNtStudyTeachDetail.getStt_id() + "getui") != null) {
                                        MemcachedUtils.replace(tdNtStudyTeachDetail.getStt_id() + "getui", tdNtTeacherGetuisCached, 3 * 60 * 60);
                                    } else {
                                        MemcachedUtils.add(tdNtStudyTeachDetail.getStt_id() + "getui", tdNtTeacherGetuisCached, 3 * 60 * 60);
                                    }
                                }
                            }
                        }
                    } else if (((i%20)==0) && ((i/10) > 5)) {
                        TdNtTeacherGetui tdNtTeacherGetui = tdNtTeacherGetuis.get(a);
                        if (tdNtTeachers.get(tdNtTeacherGetui.getTea_id()+"") == null) {
                            if (tdNtTeacherGetui.getEqu_client_id() != null && !(tdNtTeacherGetui.getEqu_client_id().equals(Constants.default_equ_id))) {
                                if (tdNtTeacherGetui.getEqu_type().equals("1")) {
                                    AndroidTeacherGetui.apnpush("NativeTalk", "One student sent you a teaching request, check now! ",
                                            tdNtTeacherGetui.getEqu_client_id(), sst_id, memb_name, memb_head_portrait
                                            , tdNtTeacherGetui.getToken(), tdNtTeacherGetui.getTea_head_portrait(), tdNtTeacherGetui.getTea_name(), cou_english_name);
                                    tdNtTeachers.put(tdNtTeacherGetui.getTea_id() + "", tdNtTeacherGetui);
                                    MemcachedUtils.add(tdNtTeacherGetui.getTea_id() + "", tdNtTeachers, 3 * 60);
                                } else {
                                    logger.info(tdNtTeacherGetui.getEqu_client_id());
                                    IosTeacherGetui.apnpush("NativeTalk", "One student sent you a teaching request, check now! ",
                                            tdNtTeacherGetui.getEqu_client_id(), sst_id, memb_name, memb_head_portrait
                                            , tdNtTeacherGetui.getToken(), tdNtTeacherGetui.getTea_head_portrait(), tdNtTeacherGetui.getTea_name(), cou_english_name);
                                    tdNtTeachers.put(tdNtTeacherGetui.getTea_id() + "", tdNtTeacherGetui);
                                    MemcachedUtils.add(tdNtTeacherGetui.getTea_id() + "", tdNtTeachers, 3 * 60);
                                }
                                tdNtTeacherGetuisCached.add(tdNtTeacherGetui);
                                if (MemcachedUtils.get(tdNtStudyTeachDetail.getStt_id() + "getui") != null) {
                                    MemcachedUtils.replace(tdNtStudyTeachDetail.getStt_id() + "getui", tdNtTeacherGetuisCached, 3 * 60 * 60);
                                } else {
                                    MemcachedUtils.add(tdNtStudyTeachDetail.getStt_id() + "getui", tdNtTeacherGetuisCached, 3 * 60 * 60);
                                }
                            }

                        }
                        a = a + 1;
                        System.out.println(a);
                        if (a >= tdNtTeacherGetuis.size()) {
                            a = tdNtTeacherGetuis.size()-1;
                        }
                        System.out.println(tdNtTeacherGetuis.size());
                    }
                }
            } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
        }



    }

}
